﻿using System;

namespace Callisto.Infrastructure
{
    public static class ServiceProviderExtensions
    {
        public static T GetService<T>(this IServiceProvider source)
        {
            var service = source.GetServiceOrFirst<T>();
            if (service == null)
                throw new InvalidOperationException(string.Format("Could not locate service of type '{0}'.", typeof(T)));

            return service;
        }

        public static T GetServiceOrFirst<T>(this IServiceProvider source)
        {
            if (source == null)
                throw new ArgumentNullException("source");

            return (T)source.GetService(typeof(T));
        }
    }
}
